<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <div class="text item">
        <!-- 搜索框区域 -->
        <el-row :gutter="20">
          <el-col :span="7">
            <el-input
              placeholder="请输入内容"
              clearable
              v-model="queryInfo.query"
              @clear="getUserList"
            >
              <el-button
                slot="append"
                @click="getUserList"
                icon="el-icon-search"
              ></el-button>
            </el-input>
          </el-col>
        </el-row>
        <!-- 用户列表区 -->
        <el-table :data="userlist" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column label="姓名" prop="username"></el-table-column>
          <el-table-column label="密码" prop="password"></el-table-column>
          <el-table-column label="级别" prop="user_level"></el-table-column>
          <el-table-column label="状态">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.state"
                :inactive-value="0"
                :active-value="1"
                @change="userStateChanged(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180px">
            <template v-slot="scope">
              <!-- 修改 -->
              <el-button
                style="margin-left: 10px"
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showEditDialog(scope.row)"
              ></el-button>
              <!-- 删除 -->
              <el-popconfirm
                title="确认删除用户？"
                style="margin-left: 30px"
                @confirm="deleteUser(scope.row)"
              >
                <el-button
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="queryInfo.pageSize"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 修改用户的对话框 -->
    <el-dialog
      title="修改用户"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClose"
    >
      <!-- 主体区域 -->
      <el-form :model="editForm" ref="editFormRef" label-width="70px">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
      </el-form>
      <!-- 页脚区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { pagingUsers } from '../../api/user';
import { deleteUser, modifyUser, selectUser } from '../../api/admin';
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 5,
      },
      userlist: [],
      total: 0,
      editForm: {
        username: '',
        password: '',
        user_id: '',
      },
      editDialogVisible: false,
    };
  },
  methods: {
    async getUserList() {
      const res = await pagingUsers({
        username: this.queryInfo.query,
        offset: this.queryInfo.pageNum,
        limit: this.queryInfo.pageSize,
      });
      this.userlist = res.data.rows;
      this.total = res.data.count;
    },
    async userStateChanged(row) {
      const res = await modifyUser(row);
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '修改用户状态成功',
        });
      }
    },
    handleSizeChange(size) {
      this.queryInfo.pageSize = size;
      this.getUserList();
    },
    handleCurrentChange(page) {
      this.queryInfo.pageNum = page;
      this.getUserList();
    },
    editDialogClose() {
      this.$refs.editFormRef.resetFields();
    },
    async editUser() {
      const res = await modifyUser(this.editForm);
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '修改用户成功',
        });
        this.getUserList();
        this.editDialogVisible = false;
      }
    },
    async showEditDialog(row) {
      const { data: res } = await selectUser(row.user_id);
      this.editForm.username = res.username;
      this.editForm.password = res.password;
      this.editForm.user_id = res.user_id;
      this.editDialogVisible = true;
    },
    async deleteUser(row) {
      const res = await deleteUser(row.user_id);
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '删除用户成功',
        });
        this.getUserList();
      }
    },
  },
  created() {
    this.getUserList();
  },
};
</script>
